package com.hntxk.cqzd.diacrisis.service.impl;

import com.hntxk.core.framework.model.ResultModel;
import com.hntxk.core.framework.service.impl.BaseServiceImpl;
import com.hntxk.cqzd.diacrisis.entity.TblDiacrisisLive;
import com.hntxk.cqzd.diacrisis.model.DiacrisisAllModel;
import com.hntxk.cqzd.diacrisis.service.DiacrisisLiveService;
import com.hntxk.sp.log.model.ActionLog;
import com.hntxk.sp.log.model.ActionType;
import com.hntxk.sp.util.LogRecordUtil;
import com.hntxk.sp.util.LoginUserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("diacLiveService")
@Transactional(rollbackFor = Exception.class)
public class DiacrisisLiveServiceImpl extends BaseServiceImpl<DiacrisisAllModel> implements DiacrisisLiveService {

    private static String MAPPER_NAMESPACE="com.hntxk.cqzd.diacrisis.mapper.DiacrisisLiveMapper.";

    public DiacrisisLiveServiceImpl() {
        super(MAPPER_NAMESPACE);
    }


    @Override
    public ResultModel<DiacrisisAllModel> insert(DiacrisisAllModel diacrisisAllModel) {

        //获取model中的Assay数据  设置主键值和外键值
        List<TblDiacrisisLive> patientlive = diacrisisAllModel.getPatientlive();
        if(patientlive.size()>0){
            for(TblDiacrisisLive live: patientlive){
                getPrimaryKey(live);
                live.setDiacrisisId(diacrisisAllModel.getTblDiacrisisInfo().getDiacrisisId());
                insert(MAPPER_NAMESPACE+"insert",live);
                if (LoginUserUtil.isLogin()) {
                    LogRecordUtil.addLog(new ActionLog(TblDiacrisisLive.class, live.getPatientLiveId() + "", "12",
                            ActionType.ADD, "新增居住环境信息：" + live.getPatientLiveId() + "[ " + live.getPatientLiveName() + " ]"));
                }

            }
        }
        return ResultModel.empty();
    }


    @Override
    public ResultModel<DiacrisisAllModel> update(DiacrisisAllModel diacrisisAllModel) {
        //获取外键id
        Long diacrisisId = diacrisisAllModel.getTblDiacrisisInfo().getDiacrisisId();
        List<TblDiacrisisLive> lives = getByFkId(diacrisisId);
        if(lives.size()>0){
            //根据外键删除此表的数据
            delete(MAPPER_NAMESPACE+"deleteByFkId",diacrisisId);
        }
        return insert(diacrisisAllModel);

    }



    @Override
    public List<TblDiacrisisLive> getByFkId(Long id) {
        TblDiacrisisLive tblDiacrisisLive=new TblDiacrisisLive();
        tblDiacrisisLive.setDiacrisisId(id);
        List<TblDiacrisisLive> List = (List<TblDiacrisisLive>) query(MAPPER_NAMESPACE + "getByFkId", tblDiacrisisLive);
        return List;
    }


}
